﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.ComponentModel;

#if WINDOWS_PHONE
using Microsoft.Phone.Controls;
#endif

using SBPweb.RIA.Shared.Contracts.Extensibility;

namespace SBPweb.RIA.Client.Controls.Base
{

    public class PageBase :
#if !WINDOWS_PHONE
        Page,
#else
        PhoneApplicationPage, 
#endif
        INotifyPropertyChanged
    {

        #region Private fields

        #endregion

        #region Constructor logic

        protected PageBase()
        {
        }

        #endregion

        #region Property accessors

        public ExtendableApplication Application
        {
            get
            {
                return System.Windows.Application.Current as ExtendableApplication;
            }
        }

        public IExtendable ApplicationInitialParameters
        {
            get
            {
                return Application.Get<IExtendable>("InitialParameters");
            }
        }

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods

        protected virtual void InitializeHandlers()
        {
        }

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        protected T GetValue<T>(DependencyProperty property)
        {
            return (T)base.GetValue(property);
        }

        protected void SetValue<T>(DependencyProperty property, T value)
        {
            SetValue<T>(property, value, null);
        }

        protected void SetValue<T>(DependencyProperty property, T value, string propertyName)
        {
            base.SetValue(property, value);
            if (!string.IsNullOrEmpty(propertyName))
            {
                RaisePropertyChanged(propertyName);
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        #endregion

    }

}
